﻿@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>费用报销</title>
    <link href="~/Scripts/jquery-easyui/themes/bootstrap/easyui.css" rel="stylesheet" />
    <script src="~/Scripts/jquery-easyui/jquery.min.js"></script>
    <script src="~/Scripts/jquery-easyui/jquery.easyui.min.js"></script>
    <script src="~/Scripts/jquery-easyui/locale/easyui-lang-zh_CN.js"></script>
    <link href="~/Content/themes/icon.css" rel="stylesheet" />
    <script src="~/Content/js/common.js?v=1.4"></script>
    <link href="~/Content/themes/table.css" rel="stylesheet" />
    <link href="~/Content/formstyles.css" rel="stylesheet" />
    <script type="text/javascript">
        var viewdata = {
            view: 'RDGrid',
            keyCode: 'RDIndex',
        }
        var lock = false;
        var columns = [[
              { field: 'id', title: '编号', sortable: false,hidden: true },          
              { field: 'claimTime', title: '单据日期', sortable: false, formatter: getDate },
               { field: 'period', title: '期间', width: 60, hidden: false },
              { field: 'claimBy', title: '报销人', sortable: false, hidden: false },
              { field: 'claimDept', title: '所在部门', width: 100, sortable: false, hidden: false },
              { field: 'total', title: '合计', width: 100, align: 'center', sortable: false, formatter: getCurrency, },
              { field: 'remark', title: '摘要', width: 200, formatter: showLongString, },
              { field: 'status', title: '状态', width: 60, hidden: false, formatter:function (value, row, index) {
                        if (value=="1") { return "已审核" } else { return "待审核" }      },
              },
              { field: 'createBy', title: '创建人', width: 60, hidden: false },
            { field: 'createTime', title: '创建时间', sortable: true, width: 70, formatter: getDate, hidden: false },
            { field: 'updateBy', title: '更新人', width: 60, hidden: false },
            { field: 'updateTime', title: '更新时间', sortable: true, width: 70, formatter: getDate, hidden: false },
        ]];
        //主表自动加载函数
        $(function () {          
            $.ajax({     //请求当前用户可以操作的按钮
                url: "/Button/GetUserAuthorizeButton?r=" + Math.random(),
                type: "post",
                data: { "KeyCode": "ExIndex", "KeyName": "ExIndex" },
                dataType: "json",
                timeout: 5000,
                success: function (data) {
                    if (data.success) {
                        //前端添加按钮
                        data.toolbar.push({ "text": "查看", "id": "lookbill", "iconCls": "icon-more", "handler": "lookbill();" })
                        var toolbar = getToolBar(data);      //common.js
                        if (data.search) {     //判断是否有浏览权限 
                            $("#ui_main_dg").datagrid({       //初始化datagrid
                                url: "/Financial/GetExpenseList?&r=" + Math.random(),
                                striped: true, rownumbers: true, pagination: true, pageSize: 20, singleSelect: true,
                                queryParams: {
                                    "view": viewdata.view,
                                },
                                idField: 'id',
                                sort: 'updateTime',
                                order: 'desc',
                                pageList: [20, 40, 60, 80, 100],
                                frozenColumns: [[]],
                                columns: columns,
                                toolbar: toolbar.length == 0 ? null : toolbar,
                                onSelect: function (rowIndex, rowData) {
                                                        menuControl();
                                },
                                onLoadSuccess: function (data) {
                        
                                    var sum_travelFee=0, sum_entertainmentFee=0, sum_officeFee=0, sum_deduct=0, sum_total=0;
                                    for (var i = 0; i < data.rows.length; i++) {
                                        sum_total += data.rows[i].total;
                                    }
                                    //添加合计行
                                    var sum_row = { "claimDept": "汇总", "total": sum_total };                                                                
                                    $('#ui_main_dg').datagrid('appendRow', sum_row);
                                }
                            });
                        }
                        else {
                            $.show_alert("提示", "无权限，请联系管理员！");
                        }
                    } else {
                        $.show_alert("错误", data.result);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    if (textStatus == "timeout") {
                        $.show_alert("提示", "请求超时，请刷新当前页重试！");
                    }
                    else {
                        $.show_alert("错误", textStatus + "：" + errorThrown);
                    }
                }
            })
            //回车搜索
            $("#ui_main_search").find('input').on('keyup', function (event) {
                if (event.keyCode == '13') {
                    ui_main_searchdata();
                }
            })
        });
        //菜单标灰处理函数
        function menuControl() {
            var rows = $("#ui_main_dg").datagrid("getChecked");        
            switch(rows[0].status) {
                case "":                   
                case "0"://未审核状态
                    $("#delete").linkbutton("enable");//允许删除
                    $("#check").linkbutton("enable");
                    $("#uncheck").linkbutton("disable");
                    $("#edit").linkbutton("enable");            
                    break;
                case "1"://已审核
                    $("#delete").linkbutton("disable");//禁止删除
                    $("#check").linkbutton("disable");
                    $("#uncheck").linkbutton("enable");                   
                    $("#edit").linkbutton("disable");
                    break;
                case 2:
                    $("#delete").linkbutton("disable");//禁止删除
                    $("#confirm").linkbutton("disable");
                    $("#unconfirm").linkbutton("disable");
                    $("#assign").linkbutton("disable");
                    $("#unassign").linkbutton("enable");
                    break;
                case 3:
                    $("#delete").linkbutton("disable");//禁止删除
                    break;
                default:
                    $("#delete").linkbutton("enable");//允许删除
                    $("#check").linkbutton("enable");
                    $("#uncheck").linkbutton("disable");
                    $("#edit").linkbutton("enable");
                    break;
            }
        }
        //显示/隐藏 toolbar按钮 
        function easyRightDisplay() {
            //获取所有的toolbar按钮
            var button = $('div.datagrid div.datagrid-toolbar a');
            for (var i = 0; i < button.length; i++) {
                var toolbar = button[i];
                var id = toolbar.id;
                if (id == "add") {  //隐藏Id为add的按钮
                    $('div.datagrid div.datagrid-toolbar a').eq(i).hide();
                }
                if (id == "delete") {  //不隐藏id为delete的按钮
                    //button.eq(i).hide();
                }
                //如果按钮都没权限，隐藏了可直接隐藏toolbar
                //$('div.datagrid div.datagrid-toolbar').hide();
            }
        }


 //新增报销单
        function AddExIndex() {
            //获取UUID
            var newid = uuid(16, 16);
            $("<div/>").dialog({
                id: "ui_main_dialog",
                href: "../views/Financial/ExpenseEdit.html",
                //title: "修改报销单",
                queryParams: { claimId: newid },
                 title: "新增报销单",
                 height: $(window).height(),
                 width: $(window).width(),
                 modal:true,
                 toolbar: [{
                    text: '保存',
                    iconCls: 'icon-save',
                    handler: function () {                      
                        if ($("#RDMainForm").form('validate')) {//表单校验
                         if (lock) { return; } else { lock = true; }
                        $.ajax({     //提交数据保存
                            url: "/Financial/AddNewExpense?r=" + Math.random(),
                            type: "post",
                            data: {
                                "claimId": newid,
                                "claimDept": $("#claimDept").val(),
                                "remark": $("#claimRemark").val(),
                                "total": $("#total").val(),
                                "period": $("#periodEdit").combobox('getText'),//$("#period").val(),                                
                                "claimBy": $("#claimBy").val(),
                                "claimTime":  $("#claimTime").datebox('getValue'),                               
                            },
                            dataType: "json",
                            timeout: 20000,
                            success: function (data) {
                                lock = false;
                                if (data.success) {
                                    $.show_alert("成功", "保存成功！");
                                    $("#ui_main_dialog").dialog('destroy'); 
                                    $("#ui_main_dg").datagrid("reload");//.datagrid('clearSelections').datagrid('clearChecked');
                                } else {
                                    $.show_alert("提示", data.msg);
                                }
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                if (textStatus == "timeout") {
                                    $.show_alert("提示", "请求超时，请刷新当前页重试！");
                                }
                                else {
                                    $.show_alert("错误", textStatus + "：" + errorThrown);
                                }
                            }
                        })
                        } else {
                            $.show_alert('操作提示', '存在校验项未通过！', "warning"); 
                        }
                    }
                }, {
                    text: '关闭',
                    iconCls: 'icon-cross',
                    handler: function () { $("#ui_main_dialog").dialog('destroy'); }
                }],
                 onLoad: function () {
                     //设置默认当前日期
                     $('#claimTime').datebox('setValue', new Date().toLocaleDateString().replace(/\//g, '-'));
                },
                onClose: function () {
                    $("#ui_main_dialog").dialog('destroy');  //销毁dialog对象
                }
            });
        }

        
        //删除项目
        function DelExIndex() {
            var rows = $("#ui_main_dg").datagrid("getChecked");
            if (rows.length < 1) {
                $.show_alert("提示", "请先勾选要删除的项目");
                return;
            }
            if (rows.length > 1) {
                $.show_alert("提示", "不支持批量删除");
                $("#ui_main_dg").datagrid('clearSelections').datagrid('clearChecked');
                return;
            }
            if (rows[0].id == null || rows[0].id == '') {
                $("#ui_main_dg").datagrid('clearSelections').datagrid('clearChecked');
                return;
            }
            $.messager.confirm('提示', '确定删除：' + rows[0].claimTime +rows[0].claimBy+ '？', function (r) {
                if (r) {
                    $.ajax({
                        url: "/Financial/DelExpenseByID?r=" + Math.random(),
                        data: {
                            ID: rows[0].id
                        },
                        type: "POST",
                        dataType: "json",
                        success: function (data) {
                            if (data.success) {
                                $.show_alert("提示", data.msg);
                                $("#ui_main_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                            } else {
                                $.show_alert("提示", data.msg);
                            }
                        }
                    });
                }
            });
        }
        //查看报销单
        function lookbill() {

            var row = $("#ui_main_dg").datagrid("getSelected");
            if (row == null || row.length < 1) {
                $.show_alert("提示", "请先勾选要查看的项目");
                return;
            }           
            if (row.id == null || row.id == '') {
                $("#ui_main_dg").datagrid('clearSelections').datagrid('clearChecked');
                return;
            }
            $("<div/>").dialog({
                id: "ui_main_dialog",
                href: "../views/Financial/ExpenseEdit.html",
                title: "查看报销单",
                queryParams: { claimId: row.id },
                height: $(window).height(),
                width: $(window).width(),
                modal: true,
                toolbar: [
                {
                    text: '关闭',
                    iconCls: 'icon-cross',
                    handler: function () {
                        $("#ui_main_dialog").dialog('destroy');
                        $("#ui_main_dg").datagrid("reload");
                    }
                }],
                //修改窗口加载后
                onLoad: function () {
                    $('#claimDept').combobox('setValue', row.claimDept);
                    $('#periodEdit').combobox('setValue', row.period);
                    $("#total").textbox('setValue', row.total);
                    $("#claimBy").textbox('setValue', row.claimBy);
                    $("#claimRemark").textbox('setValue', row.remark);                    
                    $('#claimTime').datebox('setValue', row.claimTime.replace(/\//g, '-'));
                    $('#dg').datagrid("removeToolbar")//根据下标删除
                },
                //编辑窗口关闭
                onClose: function () {
                    $("#ui_main_dialog").dialog('destroy');  //销毁dialog对象                    
                }
            });



        }
        //修改报销单
        function EditExIndex() {
            var row = $("#ui_main_dg").datagrid("getSelected");
            if (row == null || row.length < 1) {
                $.show_alert("提示", "请先勾选要更新的项目");
                return;
            }
            if (row.length > 1) {
                $.show_alert("提示", "不支持批量修改项目");
                $("#ui_main_dg").datagrid('clearSelections').datagrid('clearChecked');
                return;
            }
            if (row.id == null || row.id == '') {
                $("#ui_main_dg").datagrid('clearSelections').datagrid('clearChecked');
                return;
            }
            $("<div/>").dialog({
                id: "ui_main_dialog",
                href: "../views/Financial/ExpenseEdit.html",
                title: "修改报销单",
                queryParams: {claimId: row.id },
                height: $(window).height(),
                width: $(window).width(),
                modal: true,
                toolbar: [{
                    text: '保存',
                    iconCls: 'icon-save',
                    handler: function () {
                        if (lock) { return; } else { lock = true; }
                        if ($("#RDMainForm").form('validate')) {//表单校验
                            $.ajax({     //提交数据保存
                                url: "/Financial/updateExpense",
                                type: "post",
                                data: {
                                    "ID": row.id,
                                    "claimDept": $("#claimDept").val(),
                                    "total": $("#total").val(),
                                    "claimBy": $("#claimBy").val(),
                                    "period": $("#periodEdit").combobox('getText'),
                                    "claimTime": $("#claimTime").datebox('getValue'),
                                    "claimRemark": $("#claimRemark").val(),
                                },
                                dataType: "json",
                                timeout: 5000,
                                success: function (data) {
                                    if (data.success) {
                                        $.show_alert("成功", "更新报销单数据成功！");

                                    } else {
                                        $.show_alert("提示", data.msg);
                                    }
                                },
                                error: function (XMLHttpRequest, textStatus, errorThrown) {
                                    if (textStatus == "timeout") {
                                        $.show_alert("提示", "请求超时，请刷新当前页重试！");
                                    }
                                    else {
                                        $.show_alert("错误", textStatus + "：" + errorThrown);
                                    }
                                }
                            });
                        } else {
                            $.show_alert("错误", "表单校验未通过！请注意录入数据格式，金额为两位小数");
                        }              
                        lock = false;
                    }
                },
                {
                    text: '关闭',
                    iconCls: 'icon-cross',
                    handler: function () {
                        $("#ui_main_dialog").dialog('destroy');
                        $("#ui_main_dg").datagrid("reload");
                    }
                }],
                //修改窗口加载后
                onLoad: function () {                  
                    $('#claimDept').combobox('setValue', row.claimDept);
                    $('#periodEdit').combobox('setValue', row.period);
                    $("#total").textbox('setValue', row.total);
                    $("#claimBy").textbox('setValue', row.claimBy);
                    $("#claimRemark").textbox('setValue', row.remark);
                    $('#claimTime').datebox('setValue', row.claimTime.replace(/\//g, '-'));                  
                },
                //编辑窗口关闭
                onClose: function () {
                    $("#ui_main_dialog").dialog('destroy');  //销毁dialog对象 
                    $("#ui_main_dg").datagrid("reload");//主表更新！
                }
            });

        }

        //项目状态：审核
        function check() {
            var rows = $("#ui_main_dg").datagrid("getChecked");
            if (rows.length < 1) {
                $.show_alert("提示", "请先勾选要确认的项目");
                return;
            }
            if (rows.length > 1) {
                $.show_alert("提示", "不支持批量处理");
                $("#ui_main_dg").datagrid('clearSelections').datagrid('clearChecked');
                return;
            }
            $.messager.confirm('提示', '审核该单据：' + rows[0].remark + '？', function (r) {
                if (r) {
                    $.ajax({
                        url: "/Financial/CheckExpenseByID?r=" + Math.random(),
                        data: {
                            ID: rows[0].id,
                           // confirm: "confirm"
                        },
                        type: "POST",
                        dataType: "json",
                        success: function (data) {
                            if (data.success) {
                                $.show_alert("提示", data.msg);
                                $("#ui_main_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                                ////如果“用户管理”标签页处于打开状态需要刷新，否则被删除的角色还显示，再点编辑用户就出错
                                //if ($('#tabs').tabs('exists', '用户管理')) {
                                //    $('#tabs').tabs('getTab', '用户管理').panel('refresh');
                                //}

                            } else {
                                $.show_alert("提示", data.msg);
                            }
                        }
                    });
                }
            });
        }
        //取消审核项目
        function uncheck() {
            var rows = $("#ui_main_dg").datagrid("getChecked");
            if (rows.length < 1) {
                $.show_alert("提示", "请先勾选要取消审核的项目");
                return;
            }
            if (rows.length > 1) {
                $.show_alert("提示", "不支持批量处理");
                $("#ui_main_dg").datagrid('clearSelections').datagrid('clearChecked');
                return;
            }
            $.messager.confirm('提示', '取消审核该项目：' + rows[0].remark + '？', function (r) {
                if (r) {
                    $.ajax({
                        url: "/Financial/UnCheckExpenseByID?r=" + Math.random(),
                        data: {
                            ID: rows[0].id,
                           // confirm: "unconfirm"
                        },
                        type: "POST",
                        dataType: "json",
                        success: function (data) {
                            if (data.success) {
                                $.show_alert("提示", data.msg);
                                $("#ui_main_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');                            

                            } else {
                                $.show_alert("提示", data.msg);
                            }
                        }
                    });
                }
            });
        }      
        function ui_main_searchdata() {
            $("#ui_main_dg").datagrid('load', {
                claimBy: $('#s-claimBy').val(),
                remark: $('#s-remark').val(),
                status: $('#s-status').val(),
                period: $('#s-period').val(),
                //CreateTimeS: $('#txtAddBeginDate').datetimebox('getValue'),
                //CreateTimeE: $('#txtAddEndDate').datetimebox('getValue')
            });
            // $("#ui_main_dg").datagrid('clearSelections').datagrid('clearChecked');
        }

        function ui_main_cleardata() {
            $('#ui_main_search input').val('');
            $('#ui_main_search select').val('select');
            //$('#txtAddBeginDate').datetimebox('setValue', '');
            //$('#txtAddEndDate').datetimebox('setValue', '');
           // $("#ui_main_dg").datagrid('load', {});//清空条件自动加载数据

            //  $("#ui_main_dg").datagrid('clearSelections').datagrid('clearChecked');
        }
     

      
    </script>
</head>
<body>
    <div id="ui_main_layout" class="easyui-layout" data-options="fit:true,border:false">
        <div data-options="region:'north',split:true,border:true,collapsed:true" title="条件搜索" style="height:104px;">
            <div id="ui_main_search">
                <table class="tableForm" style="width: 99%; height: 100%; background: #F5F5F5;">
                    <tr>
                        <th>
                            报销人：
                        </th>
                        <td>
                            <input id="s-claimBy" class="easyui-textbox" style="width:150px;height:22px;" />
                        </td>
                        <th>
                            摘要：
                        </th>
                        <td>
                            <input id="s-remark" class="easyui-textbox" style="width:150px;height:22px;" />
                        </td>

                        <td>
                            <input class="easyui-combobox" id="s-status" data-options="editable:false,width:200,
                                 label:'单据状态：',labelPosition:'before',labelAlign:'right',
                                   valueField: 'value', textField: 'label',data: [
                                   {    label: '全部',
			                            value: 'all'
		                            },{
			                            label: '待审核',
			                            value: '0'
		                            },{
			                            label: '已审核',
			                            value: '1'
		                            },]" />                
                        </td>

                    <tr>                        
                        <td>
                          <input class="easyui-combobox" id="s-period" data-options="editable:false,
                                width:200,
                           label:'财务期间：',
                           labelPosition:'before',labelAlign:'right',
                               valueField: 'InfoName', textField: 'InfoName',url: '/Common/SelectBasicInfo?p=815' ">  
                        </td>
                      
                        <td colspan="4">
                            <a href="javascript:void(0);" class="easyui-linkbutton" iconcls="icon-search" plain="true"
                               onclick="ui_main_searchdata();">搜索</a>
                            <a href="javascript:void(0);" class="easyui-linkbutton" iconcls="icon-clear" plain="true"
                               onclick="ui_main_cleardata();">清空条件</a>
                        </td>
                    </tr>
                </table>
            </div>
        </div>
        <div data-options="region:'center',border:false">
            <table id="ui_main_dg" data-options="fit:true,border:false"></table>
        </div>
    </div>
</body>
</html>
